// $codepro.audit.disable
/* Copyright (c) 2011 Team A CS2340. All rights reserved.*/

/**
 * Make an user interface for doctor and nurse
 * 
 * @author Bruce
 * @version 1.0
 */
package edu.gatech.teama.doctorsoffice.client.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.SwingConstants;

import edu.gatech.teama.doctorsoffice.client.presenter.AppointmentLookupPresenter;
import edu.gatech.teama.doctorsoffice.client.presenter.PatientMainPresenter;
import edu.gatech.teama.doctorsoffice.model.*;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;

import net.miginfocom.swing.MigLayout;
import javax.swing.JComboBox;
/**
 * PatientMainPanel.java
 * @author unknown
 * @version 1.0 
 */
public class PatientMainPanel extends JPanel {
	/**
	 * myPresenter
	 */
	private PatientMainPresenter myPresenter;
	/**
	 * p
	 */

	private PatientInstructionsPanel instructionsPanel;

	private Patient p;
	/**
	 * lblStreet
	 */
	private JLabel lblStreet;

	private List<Appointment> appointments;

	/**
	 * patient main panel
	 */
	public PatientMainPanel() {
		try{
			setLayout(new GridLayout(2, 2, 20, 20));
			myPresenter = new PatientMainPresenter();
			p=(Patient)myPresenter.getLoggedUser();
			JPanel welcome = new JPanel();
			add(welcome);
			welcome.setLayout(new GridLayout(6, 2, 5, 0));

			JLabel lblWelcome = new JLabel("Welcome");
			lblWelcome.setHorizontalAlignment(SwingConstants.CENTER);
			welcome.add(lblWelcome);

			JLabel lblName = new JLabel("Name");
			lblName.setHorizontalAlignment(SwingConstants.CENTER);
			welcome.add(lblName);
			lblName.setText(p.getLastName()+" "+p.getFirstName());

			JLabel lblAddress = new JLabel("Address");
			lblAddress.setHorizontalAlignment(SwingConstants.CENTER);
			welcome.add(lblAddress);

			lblStreet = new JLabel("Street");
			lblStreet.setHorizontalAlignment(SwingConstants.CENTER);
			welcome.add(lblStreet);
			if(p.getAddress()!=null)
				lblStreet.setText(p.getAddress().getStreetName());

			JPanel emptyPanel = new JPanel();
			welcome.add(emptyPanel);

			JLabel lblCitystatezip = new JLabel("City,State,zip");
			lblCitystatezip.setHorizontalAlignment(SwingConstants.CENTER);
			welcome.add(lblCitystatezip);
			lblCitystatezip.setText(p.getAddress().getCity()+","+p.getAddress().getState());

			JLabel lblPhone = new JLabel("Phone Number");
			lblPhone.setHorizontalAlignment(SwingConstants.CENTER);
			welcome.add(lblPhone);

			JLabel lblNumber = new JLabel("##");
			lblNumber.setHorizontalAlignment(SwingConstants.CENTER);
			welcome.add(lblNumber);
			lblNumber.setText(p.getPhoneNumber());

			JLabel lblEmail = new JLabel("Email");
			lblEmail.setHorizontalAlignment(SwingConstants.CENTER);
			welcome.add(lblEmail);

			JLabel lblEAddress = new JLabel("name@host.com");
			lblEAddress.setHorizontalAlignment(SwingConstants.CENTER);
			welcome.add(lblEAddress);
			lblEAddress.setText(p.getEmailAddress());
			//set up personal info



			JPanel bottunPanel = new JPanel();
			add(bottunPanel);
			bottunPanel.setLayout(new GridLayout(2, 1, 0, 20));

			JLabel toDo = new JLabel("What Would You Like To Do?");
			toDo.setHorizontalAlignment(SwingConstants.CENTER);
			bottunPanel.add(toDo);

			JButton btnRequest = new JButton("Request Appointment");
			btnRequest.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					myPresenter.requestApt();
				}
			});
			bottunPanel.add(btnRequest);

			JPanel upComingApt = new JPanel();
			add(upComingApt);
			upComingApt.setLayout(new MigLayout("", "[grow][]", "[][][grow]"));

			JLabel lblAppointments = new JLabel("Appointments");
			upComingApt.add(lblAppointments, "cell 0 0");

			appointments = myPresenter.getAppointments();

			final JComboBox comboBox = new JComboBox();


			for (Appointment appointment : appointments) {
				int reasonLength = appointment.getReason().length();
				if (reasonLength > 15)
					reasonLength = 15;
				comboBox.addItem(appointment.getDate().toString("MM/dd/yyyy HH:mm") + " - "
						+ appointment.getReason().substring(0, reasonLength) + "...");
			}

			upComingApt.add(comboBox, "cell 0 1,growx");

			JPanel panel_1 = new JPanel();
			upComingApt.add(panel_1, "cell 0 2,grow");

			final JButton btnEdit = new JButton("Edit");
			btnEdit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

				}
			});
			panel_1.add(btnEdit);

			final JButton btnCancelAppointment = new JButton("Cancel Appointment");
			btnCancelAppointment.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

				}
			});
			panel_1.add(btnCancelAppointment);
			final JButton btnViewDoctorsOrders = new JButton("View Appointment");

			btnViewDoctorsOrders.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (comboBox.getSelectedIndex() >= 0) {
						Appointment selected = appointments.get(comboBox.getSelectedIndex());
						instructionsPanel = new PatientInstructionsPanel(selected.getTreatmentRecord());
						JFrame tempFrame= new JFrame();
						tempFrame.getContentPane().add(instructionsPanel);
						instructionsPanel.setEditable(false);
						instructionsPanel.setVisible(true);
						tempFrame.setSize(900,600);
						tempFrame.setVisible(true);
					}
				}
			});
			panel_1.add(btnViewDoctorsOrders);

			JPanel panel = new JPanel();
			add(panel);
			panel.setLayout(new GridLayout(4, 2, 0, 5));

			JLabel lblPresentAppointment = new JLabel("Present Appointment");
			lblPresentAppointment.setHorizontalAlignment(SwingConstants.CENTER);
			//panel.add(lblPresentAppointment);

			JPanel present1 = new JPanel();
			//panel.add(present1);
			present1.setLayout(new GridLayout(2, 2, 5, 5));

			JLabel lblDate_1 = new JLabel("Date");
			lblDate_1.setHorizontalAlignment(SwingConstants.CENTER);
			lblDate_1.setEnabled(true);
			present1.add(lblDate_1);

			JLabel lblShowDate2 = new JLabel("11/11/11");
			lblShowDate2.setHorizontalAlignment(SwingConstants.CENTER);
			present1.add(lblShowDate2);

			JLabel lblReason_1 = new JLabel("Reason");
			lblReason_1.setHorizontalAlignment(SwingConstants.CENTER);
			present1.add(lblReason_1);

			JLabel lblshowreason = new JLabel("sick");
			lblshowreason.setHorizontalAlignment(SwingConstants.CENTER);
			present1.add(lblshowreason);

			JPanel present2 = new JPanel();
			//panel.add(present2);
			present2.setLayout(new GridLayout(2, 2, 5, 5));

			JLabel lblDate_2 = new JLabel("Date");
			lblDate_2.setHorizontalAlignment(SwingConstants.CENTER);
			present2.add(lblDate_2);

			JLabel lblShowDate3 = new JLabel("11/11/11");
			lblShowDate3.setHorizontalAlignment(SwingConstants.CENTER);
			present2.add(lblShowDate3);

			JLabel lblReason_2 = new JLabel("Reason");
			lblReason_2.setHorizontalAlignment(SwingConstants.CENTER);
			present2.add(lblReason_2);

			JLabel lblShowReason3 = new JLabel("sick");
			lblShowReason3.setHorizontalAlignment(SwingConstants.CENTER);
			present2.add(lblShowReason3);

			JButton btnLogOut = new JButton("Log Out");
			btnLogOut.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					myPresenter.logout();
				}
			});
			panel.add(btnLogOut);
			
			if (comboBox.getSelectedIndex() >= 0 && appointments.size() > comboBox.getSelectedIndex()) {
				Appointment original=appointments.get(comboBox.getSelectedIndex());
				if (original.getDate().isBefore(new LocalDateTime())) {
					btnEdit.setVisible(false);
					btnCancelAppointment.setVisible(false);
					btnViewDoctorsOrders.setVisible(true);
				}
				else {
					btnEdit.setVisible(true);
					btnCancelAppointment.setVisible(true);
					btnViewDoctorsOrders.setVisible(false);

				}
			}
			comboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (comboBox.getSelectedIndex() >= 0) {
						Appointment selected = appointments.get(comboBox.getSelectedIndex());
						if (selected.getDate().isBefore(new LocalDateTime())) {
							btnEdit.setVisible(false);
							btnCancelAppointment.setVisible(false);
							btnViewDoctorsOrders.setVisible(true);
						}
						else {
							btnEdit.setVisible(true);
							btnCancelAppointment.setVisible(true);
							btnViewDoctorsOrders.setVisible(false);

						}
					}
				}
			});

		}catch(NullPointerException e)
		{
			myPresenter.logout();
			System.out.println("patient info not complet");
		}
		initDataBindings();

	}
	/**
	 * initial data bindings
	 */
	protected void initDataBindings() {
		BeanProperty<Patient, String> patientBeanProperty = BeanProperty.create("address.streetName");
		BeanProperty<JLabel, String> jLabelBeanProperty = BeanProperty.create("text");
		AutoBinding<Patient, String, JLabel, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ, p, patientBeanProperty, lblStreet, jLabelBeanProperty);
		autoBinding.bind();
	}
}
